import React from 'react'
import router from 'umi/router'
import { connect } from 'dva'
import { Layout } from 'antd'
import LOGINOUT from 'assets/workBench/loginout.png'
import './index.styl'

function Header({ login: { userMsg }, dispatch }) {
  function loginOut() {
    sessionStorage.clear()
    router.push('/login')
    sessionStorage.removeItem('loginInfo')
  }

  const localUserMsg = localStorage.getItem('userMsg') ? JSON.parse(localStorage.getItem('userMsg')) : null


  // 超过8小时没有操作跳转登录页面
  function hasOperate(callback, second) { //second是检测未操作的时间，秒为单位，callback是该时间段未点击需要执行的函数
    let status = true;
    let timer = null;
    document.body.onmousedown = function () {
      status = true;
    }
    document.body.onmouseup = function () {
      countTime();
    }
    function countTime() {
      setInterval(function () {
        if (!status) {
          callback();
          status = true;
        }
      }, 1);
      if (timer) {
        clearInterval(timer);
      }
      timer = setInterval(function () {
        status = false;
      }, second);
    }
    countTime();
  }

  function aaa() {
    // 清空用户信息缓存
    localStorage.clear()
    router.push('/login')
  }
  hasOperate(aaa, 1000 * 60 * 60 * 8);

  return (
    <Layout.Header className="self-header">
      <div className="rightPanel" style={{ fontSize: '16px', color: '#666666' }}>{userMsg && userMsg.companyName || localUserMsg && localUserMsg.companyName}</div>
      <div className="rightPanel">
        {
          (userMsg && userMsg.headImage) || (localUserMsg && localUserMsg.headImage)?
          <img className="avater" alt={''} src={userMsg && userMsg.headImage || localUserMsg && localUserMsg.headImage} />
          :
          <b className="no-avater">
          </b>
        }
        <span>
          {userMsg && userMsg.name || localUserMsg && localUserMsg.name}
        </span>
        <img src={LOGINOUT} onClick={loginOut} />
      </div>
    </Layout.Header>
  )
}

export default connect(({ login, headerTaps }) => ({
  login, headerTaps
}))(Header)
